package com.atguigu.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class RateLimitController {

    @GetMapping("byResource")
    @SentinelResource(value = "bySentinelResource", blockHandler = "doActionBlocckHandler", fallback = "doActionFollBackHandler")
    public String byResource() {
        int a = 10 / 0;
        return "安装资源名称SentinelResource限流测试OK";
    }

    /**
     * 热点参数限流
     *
     * @param p1
     * @param p2
     * @return
     */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey", blockHandler = "dealHandlerTotesthotkey")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        return "------testHotKey";
    }

    public String dealHandlerTotesthotkey(String p1, String p2, BlockException blockException) {
        return "触发限流";
    }

    public String doActionBlocckHandler(BlockException blockException) {
        return "触发限流";
    }

    public String doActionFollBackHandler(Throwable throwable) {
        return "发生代码异常";
    }
}
